package com.bootdo.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.common.utils.StrUtil;
import com.bootdo.system.config.KswApi;
import com.bootdo.system.dto.CzParm;
import com.bootdo.system.dto.GmspDto;
import com.bootdo.system.dto.Order2GoodsDto;
import com.bootdo.system.dto.OrderDto;
import com.bootdo.system.dto.ProductDto;
import com.bootdo.system.dto.ResultDto;
import com.bootdo.system.dto.SjxxDto;
import com.bootdo.system.service.KswService;
import net.sf.json.JSONArray;

@Service
public class KswServiceImpl implements KswService{

	@Override
	public ProductDto getSpxx(int product_id, String key, TreeMap<String, String> treeMap) {
			String timestamp = StrUtil.getTimestamp();
			String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "product_id" + product_id+"timestamp"+timestamp);
		    treeMap.put("product_id" , product_id+"");
		    treeMap.put("sign" , md5);
		    String s = HttpUtil.doPost(KswApi.KSW_PRODUCT,treeMap,null);
		    System.out.println(s);
		    Map maps = (Map)JSON.parse(s);  
		    String code = (String) maps.get("code");
		    if(!("ok").equals(code)) {
		    	return null;
		    }
		return JSON.parseObject(JSON.toJSONString(maps.get("data")), ProductDto.class);
	}


	@Override
	public CzParm getCzParm(int product_id, String key, TreeMap<String, String> treeMap) {
			String timestamp = StrUtil.getTimestamp();
			String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "product_id" + product_id+"timestamp"+timestamp);
		    treeMap.put("product_id" , product_id+"");
		    String s = HttpUtil.doPost(KswApi.KSW_PRODUCT_RECHARGE_PARAMS,treeMap,null);
		    Map maps = (Map)JSON.parse(s);  
	    return JSON.parseObject(JSON.toJSONString(maps.get("data")), CzParm.class);
	}

	@Override
	public GmspDto getGmspDto(int product_id, String recharge_account,
			int quantity, String key, TreeMap<String, String> treeMap) {
			String timestamp = StrUtil.getTimestamp();
			String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "product_id" + product_id+ "quantity" + quantity+ "recharge_account" + recharge_account+ "timestamp"+timestamp);
		    treeMap.put("product_id" , product_id+"");
		    treeMap.put("recharge_account" , recharge_account);
		    treeMap.put("quantity" , quantity+"");
		    treeMap.put("sign" , md5);
		    String s = HttpUtil.doPost(KswApi.KSW_BUY,treeMap,null);
		    System.out.println(s);
		    Map maps = (Map)JSON.parse(s);  
		    String code = (String) maps.get("code");
		    if(!("ok").equals(code)) {
		    	return null;
		    }
	    return JSON.parseObject(JSON.toJSONString(maps.get("data")), GmspDto.class);
	}


	@Override
	public OrderDto getOrderDto(Integer order_id, String key, TreeMap<String, String> treeMap) {
			String timestamp = StrUtil.getTimestamp();
			String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "order_id" + order_id+"timestamp"+timestamp);
		    treeMap.put("order_id" , order_id+"");
		    String s = HttpUtil.doPost(KswApi.KSW_ORDER,treeMap,null);
		    Map maps = (Map)JSON.parse(s);  
	    return JSON.parseObject(JSON.toJSONString(maps.get("data")), OrderDto.class);
	}


	@Override
	public SjxxDto getSjxxDto(int customer_id, String key, TreeMap<String, String> treeMap) {
		String timestamp = StrUtil.getTimestamp();
		String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+"timestamp"+timestamp);
	    String s = HttpUtil.doPost(KswApi.KSW_CUSTOMER,treeMap,null);
	    Map maps = (Map)JSON.parse(s);  
    return JSON.parseObject(JSON.toJSONString(maps.get("data")), SjxxDto.class);
	}


	@Override
	public Order2GoodsDto getOrder2GoodsDto(int customer_id, String key, TreeMap<String, String> treeMap) {
		String timestamp = StrUtil.getTimestamp();
		String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "timestamp"+timestamp);
	    String s = HttpUtil.doPost(KswApi.KSW_SUPPLY_API_SUPPLY_ORDERS,treeMap,null);
	    Map maps = (Map)JSON.parse(s);  
    return JSON.parseObject(JSON.toJSONString(maps.get("data")), Order2GoodsDto.class);
	}


	@Override
	public ResultDto doOrder(Integer order_id, String action, int progress_init, int progress_now, int progress_target,
			String recharge_info, String cards, String key, TreeMap<String, String> treeMap) {
		String timestamp = StrUtil.getTimestamp();
		String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "order_id" + order_id+ "action" + action+ "progress_init" + progress_init+ "progress_now" + progress_now+ "progress_target" + progress_target+ "recharge_info" + recharge_info+ "cards" + cards+"timestamp"+timestamp);
	    treeMap.put("order_id" , order_id+"");
	    treeMap.put("action" , action);
	    treeMap.put("progress_init" , progress_init+"");
	    treeMap.put("progress_now" , progress_now+"");
	    treeMap.put("progress_target" , progress_target+"");
	    treeMap.put("recharge_info" , recharge_info);
	    treeMap.put("cards" , cards);
	    String s = HttpUtil.doPost(KswApi.KSW_SUPPLY_API_SUPPLY_ORDERS,treeMap,null);
	    Map maps = (Map)JSON.parse(s);  
    return JSON.parseObject(JSON.toJSONString(s), ResultDto.class);
	}


	@Override
	public ResultDto batchDoOrder(List<Map<String,Object>> data, String key, TreeMap<String, String> treeMap) {
		String timestamp = StrUtil.getTimestamp();
		String data1 = JSONArray.fromObject(data).toString();
		String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "data" + data1+"timestamp"+timestamp);
	    treeMap.put("data" , data1);
	    String s = HttpUtil.doPost(KswApi.KSW_SUPPLY_BATCH_HANDLE_API_SUPPLY_ORDER,treeMap,null);
	    Map maps = (Map)JSON.parse(s);  
    return JSON.parseObject(JSON.toJSONString(s), ResultDto.class);
	}


	@Override
	public ResultDto doOrder2tk(int order_id, double refund_amount, String key, TreeMap<String, String> treeMap) {
		String timestamp = StrUtil.getTimestamp();
		String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "order_id" + order_id+ "refund_amount" + refund_amount+"timestamp"+timestamp);
	    treeMap.put("order_id" , order_id+"");
	    treeMap.put("refund_amount" , refund_amount+"");
	    String s = HttpUtil.doPost(KswApi.KSW_SUPPLY_REFUND_ORDER,treeMap,null);
	    Map maps = (Map)JSON.parse(s);  
    return JSON.parseObject(JSON.toJSONString(s), ResultDto.class);
	}


	@Override
	public ResultDto editGoodsPrice(int product_id, double cost, double price, String key,
			TreeMap<String, String> treeMap) {
		String timestamp = StrUtil.getTimestamp();
		String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "product_id" + product_id+ "cost" + cost+ "price" + price+"timestamp"+timestamp);
	    treeMap.put("product_id" , product_id+"");
	    treeMap.put("cost" , cost+"");
	    treeMap.put("price" , price+"");
	    String s = HttpUtil.doPost(KswApi.KSW_SUPPLY_PRODUCT_SET_PRICE,treeMap,null);
	    Map maps = (Map)JSON.parse(s);  
    return JSON.parseObject(JSON.toJSONString(s), ResultDto.class);
	}


	@Override
	public ResultDto editGhsStatus(int product_id, int supply_state, String maintenance_info, String key,
			TreeMap<String, String> treeMap) {
		String timestamp = StrUtil.getTimestamp();
		String md5 = StrUtil.getMd5(key +"customer_id" + treeMap.get("customer_id")+ "product_id" + product_id+ "supply_state" + supply_state+ "maintenance_info" + maintenance_info+"timestamp"+timestamp);
	    treeMap.put("product_id" , product_id+"");
	    treeMap.put("supply_state" , supply_state+"");
	    treeMap.put("maintenance_info" , maintenance_info);
	    String s = HttpUtil.doPost(KswApi.KSW_SUPPLY_PRODUCT_SET_SUPPLY_STATE,treeMap,null);
	    Map maps = (Map)JSON.parse(s);  
    return JSON.parseObject(JSON.toJSONString(s), ResultDto.class);
	}
	



}
